// Configuration.cpp : implementation file
//

#include "stdafx.h" 
#include <stdio.h>
#include "Configuration.h"

CConfiguration* CConfiguration::m_instance;

/////////////////////////////////////////////////////////////////////
CConfiguration::CConfiguration(void)
{
}
/////////////////////////////////////////////////////////////////////
CConfiguration::~CConfiguration(void)
{
	delete m_instance;
}
/////////////////////////////////////////////////////////////////////
void CConfiguration::Load(void)
{
	try
	{
		::GetModuleFileName(NULL,m_strBasePath.GetBuffer(_MAX_PATH),_MAX_PATH);
		m_strBasePath.ReleaseBuffer();		
		m_strBasePath.Replace('/','\\');		
		m_strBasePath = m_strBasePath.Left(m_strBasePath.ReverseFind('\\') + 1);		

		
		CString pLastChar = m_strBasePath.Right(1);		
		if(pLastChar !='\\' && pLastChar != '/') 
		{
			m_strBasePath += "\\";
		}	
	
		m_strSettingsPath = m_strBasePath +  _T("Settings\\");
		if(!CheckPathExists(m_strSettingsPath))
		{
			CreateDirectory(m_strSettingsPath,NULL);
		}

		m_strSettingsFilePath = m_strSettingsPath +  _T("Settings.ini");
		m_iniFile.SetIniFileName(m_strSettingsFilePath);
		if(!CheckPathExists(m_strSettingsFilePath))
		{
			m_iniFile.WriteSection(_T("Connection"), _T("ServerUrl=http://localhost/WebSrv.asmx\x000ServerUrl1=http://localhost/WebSrv.asmx"));		
			m_iniFile.WriteSection(_T("Params"), _T("Image=tiff"));		
		}
	}
	catch(CException *ex)
	{		
		TRACE(_T("[%s]   %s\n") , "ApplicationSettings::Load", "");
		ex->ReportError();
        ex->Delete();
	}
}
/////////////////////////////////////////////////////////////////////
void CConfiguration::RecursiveDelete(CString szPath)
{
	CFileFind ff;
	CString path = szPath;
	
	if(path.Right(1) != "\\")
		path += "\\";

	path += "*.*";

	BOOL res = ff.FindFile(path);

	while(res)
	{
		res = ff.FindNextFile();
		if (!ff.IsDots() && !ff.IsDirectory())
			DeleteFile(ff.GetFilePath());
		else if (ff.IsDirectory())
		{
			path = ff.GetFilePath();
			RecursiveDelete(path);
			RemoveDirectory(path);
		}
	}
}
/////////////////////////////////////////////////////////////////////
BOOL CConfiguration::CheckPathExists(CString szPath)
{
    return (GetFileAttributes(szPath) != INVALID_FILE_ATTRIBUTES);
}
/////////////////////////////////////////////////////////////////////
CConfiguration* CConfiguration::Instance()
{
	if (m_instance == NULL) 
	{
		m_instance = new CConfiguration();
		m_instance->Load();
	}
	return m_instance;
}
/////////////////////////////////////////////////////////////////////
